package com.apache.bid.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.entity.BidItemControl;
import com.apache.bid.manager.BidItemControlManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;

public class BidItemControlManagerImpl implements BidItemControlManager {

	protected IDao bidItemControlDao;
	protected final String entityName = "com.apache.bid.entity.BidItemControl";

	public void setBidItemControlDao(IDao bidItemControlDao) {
		this.bidItemControlDao = bidItemControlDao;
	}

	public String saveInfo(ParamsVo<BidItemControl> vo) throws BusinessException {
		BidItemControl bidItemControl = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidItemControl_"+infoId;
		bidItemControl.setControlId(infoId);
		MethodParam param = new MethodParam("BidItemControl", cacheKey, "", entityName);
		param.setVaule(bidItemControl);
		if (bidItemControlDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidItemControl> vo) throws BusinessException {
		BidItemControl bidItemControl = vo.getObj();
		if (Validator.isNotNull(bidItemControl.getControlId())) {
			String cacheKey = "";
			//String cacheKey="bidItemControl_"+bidItemControl.getControlId();
			MethodParam param = new MethodParam("BidItemControl", cacheKey, "", entityName);
			param.setVaule(bidItemControl);
			return bidItemControlDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidItemControl> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="bidItemControl_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		BidItemControl info = (BidItemControl) bidItemControlDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("BidItemControl");
			return bidItemControlDao.edit(param);
		} else {
			param.setParams("controlId", infoId);
			param.setDelete(true);
			return bidItemControlDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<BidItemControl> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidItemControl_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidItemControlDao.selectById(param);
	}

	public Object execute(ParamsVo<BidItemControl> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<BidItemControl> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		String orderBy = Validator.getDefaultStr(String.valueOf(vo.getParams("orderBy")),
				" order by last_bid_time desc ");
		param.setParams("orderBy", orderBy);
		Page page = bidItemControlDao.pageSelect(param);
		return page;
	}

	public List<BidItemControl> getList(ParamsVo<BidItemControl> vo) {
		MethodParam param = setMethodParams(vo, 2);
		String orderBy = Validator.getDefaultStr(String.valueOf(vo.getParams("orderBy")),
				" order by last_bid_time desc ");
		param.setParams("orderBy", orderBy);
		return bidItemControlDao.select(param);
	}

	public long countInfo(ParamsVo<BidItemControl> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidItemControlDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidItemControl> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidItemControlDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		param.setResultCache(false);
		BidItemControl bidItemControl = vo.getObj();
		if (Validator.isNotNull(bidItemControl.getUserId())) {
			sb.append(" and userId = :userId");
			param.setParams("userId", bidItemControl.getUserId());
		}
		if (Validator.isNotNull(bidItemControl.getItemId())) {
			sb.append(" and itemId = :itemId");
			param.setParams("itemId", bidItemControl.getItemId());
		}
		if (Validator.isNotNull(bidItemControl.getIfRight())) {
			sb.append(" and ifRight = :ifRight");
			param.setParams("ifRight", bidItemControl.getIfRight());
		}
		if (Validator.isNotNull(bidItemControl.getRightLevel())) {
			sb.append(" and rightLevel = :rightLevel");
			param.setParams("rightLevel", bidItemControl.getRightLevel());
		}
		if (Validator.isNotNull(bidItemControl.getModifyTime())) {
			sb.append(" and modifyTime = :modifyTime");
			param.setParams("modifyTime", bidItemControl.getModifyTime());
		}
		if (Validator.isNotNull(bidItemControl.getWantBuyNum())) {
			sb.append(" and wantBuyNum = :wantBuyNum");
			param.setParams("wantBuyNum", bidItemControl.getWantBuyNum());
		}
		if (Validator.isNotNull(bidItemControl.getFactBuyNum())) {
			sb.append(" and factBuyNum = :factBuyNum");
			param.setParams("factBuyNum", bidItemControl.getFactBuyNum());
		}
		if (Validator.isNotNull(bidItemControl.getLastBidPrice())) {
			sb.append(" and lastBidPrice = :lastBidPrice");
			param.setParams("lastBidPrice", bidItemControl.getLastBidPrice());
		}
		if (Validator.isNotNull(bidItemControl.getLastPrio())) {
			sb.append(" and lastPrio = :lastPrio");
			param.setParams("lastPrio", bidItemControl.getLastPrio());
		}
		if (Validator.isNotNull(bidItemControl.getLastBidTime())) {
			sb.append(" and lastBidTime = :lastBidTime");
			param.setParams("lastBidTime", bidItemControl.getLastBidTime());
		}
		if (Validator.isNotNull(bidItemControl.getAutoPriceFlag())) {
			sb.append(" and autoPriceFlag = :autoPriceFlag");
			param.setParams("autoPriceFlag", bidItemControl.getAutoPriceFlag());
		}
		if (Validator.isNotNull(bidItemControl.getAgree())) {
			sb.append(" and agree = :agree");
			param.setParams("agree", bidItemControl.getAgree());
		}
		if (Validator.isNotNull(bidItemControl.getTurn())) {
			sb.append(" and turn = :turn");
			param.setParams("turn", bidItemControl.getTurn());
		}
		if (Validator.isNotNull(bidItemControl.getSpare1())) {
			sb.append(" and spare1 = :spare1");
			param.setParams("spare1", bidItemControl.getSpare1());
		}
		if (Validator.isNotNull(bidItemControl.getSpare2())) {
			sb.append(" and spare2 = :spare2");
			param.setParams("spare2", bidItemControl.getSpare2());
		}
		if (Validator.isNotNull(bidItemControl.getSpare3())) {
			sb.append(" and spare3 = :spare3");
			param.setParams("spare3", bidItemControl.getSpare3());
		}
		if (Validator.isNotNull(bidItemControl.getSpare4())) {
			sb.append(" and spare4 = :spare4");
			param.setParams("spare4", bidItemControl.getSpare4());
		}
		if (Validator.isNotNull(bidItemControl.getSpare5())) {
			sb.append(" and spare5 = :spare5");
			param.setParams("spare5", bidItemControl.getSpare5());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
